<?php

namespace App\Http\Controllers\Common;

use App\Http\Controllers\Controller;
use App\Models\Delivery;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

//前后台可以通用的
class ProvinceController extends Controller
{
    //返回所有的省
    function provinces() {
        return DB::table('cities')->where('level','province')
                ->select('name')->get()->toArray();
    }

    //返回某省的市
    function city(Request $request) {
        $where[] = ['level', '=', 'city'];
        if ($province = $request->get('province')) {
            $where[] = ['pname', 'like' , $province . '%'];
        }
        return DB::table('cities')->where($where)->pluck('name')->toArray();
    }

    //返回某市的区
    function district(Request $request) {
        $where[] = ['level','=','district'];
        if ($city = $request->get('city')) {
            $where[] = ['pname','like',$city . '%'];
        }
        return DB::table('cities')->where($where)->pluck('name')->toArray();
    }

    //返回开通了取货门店的城市
    function delivery_cities(Request $request) {
        return $this->response(0,Delivery::distinct('city')->pluck('city')->toArray(),'');
    }
}
